home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / telecomm / uwsrc.arc / WINIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-29  |  11.5 KB  |  499 lines

  1. /*
  2.  * This file contains file io routines
  3.  */
  4. #include <obdefs.h>
  5. #include <gemdefs.h>
  6. #include <stdio.h>
  7. #include <osbind.h>
  8. #include <xbios.h>
  9. #include "wind.h"
  10. #include "uw.h"
  11. #include "windefs.h"
  12.  
  13. extern struct wi_str w[];
  14.  
  15. /*
  16.  * Character stuff (fonts, keymaps).
  17.  */
  18. extern FNT *fnttbl[10];            /* List of pointers to fonts avail */
  19. extern int fontsavail;            /* Number of fonts available */
  20. extern FNT *curfont;            /* font in use */
  21. extern int fontmenuobj[];        /* array of font menu object numbers */
  22. /*
  23.  * Options (somewhat loosely defined).
  24.  */
  25. extern int fast;            /* flag for fast open/close */
  26. extern int overstrike;            /* flag for character output */
  27. extern int sliders;            /* flag for sliders on new windows */
  28. extern int titles;            /* flag for title bars on new windows */
  29. extern int audibell;            /* Audible bell. */
  30. extern int visibell;            /* Visible bell. */
  31. extern int toponbel;            /* Top window on bell. */
  32.  
  33. extern FUNCSTRING fstrings[];        /* storage for function key bodys */
  34.  
  35. extern char cmdpath[];            /* default command executions strings */
  36. extern char cmdname[];
  37. extern char cmdargs[];
  38.  
  39. extern OBJECT *menubar;
  40.  
  41. void rsbufset();
  42. long    bufsizs[] = {        /* rs-232 buffer sizes setable from uw */
  43.     256l,    2048l,    16384l
  44. };
  45. int    bufsiz = 0;        /* index into table above of current size */
  46.  
  47. #ifdef    LOCATE
  48. extern    char    *locate();
  49. #else
  50. #define    locate(n)    n
  51. #endif
  52.  
  53. /* read a font from a file */
  54. FNT * loadfont (name)
  55. char    *name;
  56. {
  57.   FNT * curfont;
  58.   char *tmpfnt;
  59.   char menustr[20];
  60.   char namestr[20];
  61.   register int shift;
  62.   register unsigned int mask;
  63.   register int    i;
  64.   char *getmem();
  65.  
  66.   if ((curfont = (FNT *) getmem((long)sizeof(FNT))) == NULL)
  67.     return(NULL);
  68.   i = Fopen(locate(name), 0);
  69.   if (i<0)
  70.     return (NULL);
  71.   Fread(i, 2048L, curfont->f_data);
  72.   Fclose(i);
  73.   curfont->inc_x = MINMAX(curfont->f_data[16], 1, 8);
  74.   curfont->inc_y = MINMAX(curfont->f_data[32], 1, 16);
  75.   shift = 8-curfont->inc_x;
  76.   mask = (1<<curfont->inc_x)-1;
  77.   tmpfnt = curfont->f_data;
  78.   for (i=0; i<2048; i++)
  79.   {
  80.     *tmpfnt = (*tmpfnt>>shift)&mask;
  81.     tmpfnt++;
  82.   }
  83.   sscanf(name, "wind%3s", namestr);
  84.   sprintf(menustr, "%1.2d x %1.2d %s font", curfont->inc_x, curfont->inc_y,
  85.     namestr);
  86.   set_menu_string(menustr, fontmenuobj[fontsavail]);
  87.   gen_hash(curfont, &curfont->f_hash);
  88.  
  89.   fnttbl[fontsavail] = curfont;
  90.   fontsavail++;
  91.   return (curfont);
  92. }
  93.  
  94. /* This function reads the config file from name in directory path and sets
  95.  * the apropriate variables.
  96.  */
  97. read_config(path, name)
  98. char *path;
  99. char *name;
  100. {
  101.   char file[100];
  102.   char *ptr;
  103.   char *status;
  104.   FILE *fd;
  105.   int i, j;
  106.   char *index(), *rindex();
  107.   
  108.   strcpy(file, path);
  109.   ptr = rindex(file, '\\');
  110.   if (ptr != NULL)
  111.     *(ptr+1) = '\0';
  112.   strcat(file, name);
  113.   
  114.   fd = fopen(file, "r");
  115.   if (fd == NULL)
  116.   {
  117.     if (*path != '\0')    /* don't print error if in startup */
  118.       form_alert(1, "[2][Can't open config input file][ok]");
  119.     return;
  120.   }
  121.   status = fgets(file, 100,fd);
  122.   while (status != NULL)
  123.   {
  124.     char * endline;
  125.     if ((endline = rindex(file, '\n')) != NULL)
  126.       *endline = '\0';
  127.     if (!strncmp("audibell=", file, 9))
  128.       audibell = atoi(file + 9);
  129.     else if (!strncmp("visibell=", file, 9))
  130.       visibell = atoi(file + 9);
  131.     else if (!strncmp("topbell=", file, 8))
  132.       toponbel = atoi(file + 8);
  133.     else if (!strncmp("fast", file, 4))
  134.       ++fast;
  135.     else if (!strncmp("slow", file, 4))
  136.         fast = 0;
  137.     else if (!strncmp("overstrike", file, 4))
  138.       ++overstrike;
  139.     else if (!strncmp("nooverstrike", file, 4))
  140.       overstrike = 0;
  141.     else if (!strncmp("sliders", file, 7))
  142.       ++sliders;
  143.     else if (!strncmp("nosliders", file, 9))
  144.         sliders = 0;
  145.     else if (!strncmp("titles", file, 6))
  146.       ++titles;
  147.     else if (!strncmp("notitless", file, 8))
  148.         titles = 0;
  149.     else if (!strncmp("font=", file, 5))
  150.     {
  151.       i = atoi(file+5);
  152.       if (fontsavail > i)
  153.       {
  154.         for (j = 0; j < fontsavail; j++)
  155.           objc_change(menubar, fontmenuobj[j], 0, 0, 0, 0, 0, NONE, 0);
  156.         objc_change(menubar, fontmenuobj[i], 0, 0, 0, 0, 0, CHECKED, 0);
  157.         curfont = fnttbl[i];
  158.       }
  159.     }
  160.     else if (!strncmp("bufsiz=", file, 7))
  161.     {
  162.       i = atoi(file+7);
  163.       for (j=0; j < sizeof(bufsizs)/sizeof(bufsizs[0]); j++)
  164.           if (bufsizs[j] >= i)
  165.       break;
  166.       bufsiz = j;
  167.       rsbufset(j);
  168.     }
  169.     else if (file[0] == 'f' && file[1] >= '0' && file[1] <= '9'
  170.       && (file[2] == '=' || file[3] == '='))
  171.       {
  172.     i = atoi(file+1);
  173.     if (i < 1) i = 1;
  174.     if (i > NFSTRINGS - 2) i = NFSTRINGS - 2;
  175.     status = index(file, '=');
  176.     ptr = index(status, '\n');
  177.     if (ptr != NULL)
  178.       *ptr = '\0';
  179.     strcpy(fstrings[i-1], status+1);
  180.       }
  181.     else if (!strncmp("cmdpath=", file, 8))
  182.     strcpy(cmdpath, file+8);
  183.     else if (!strncmp("cmdname=", file, 8))
  184.     strcpy(cmdname, file+8);
  185.     else if (!strncmp("cmdargs=", file, 8))
  186.     strcpy(cmdargs, file+8);
  187.     status = fgets(file, 100, fd);
  188.   }
  189.   objc_change(menubar, VISIBELL, 0, 0, 0, 0, 0,
  190.     visibell? CHECKED: 0, 0);
  191.   objc_change(menubar, AUDIBELL, 0, 0, 0, 0, 0,
  192.     audibell? CHECKED: 0, 0);
  193.   objc_change(menubar, TOPONBEL, 0, 0, 0, 0, 0,
  194.     toponbel? CHECKED: 0, 0);
  195.   menu_icheck(menubar, MFAST, !fast);
  196.   menu_icheck(menubar, OVERSTRI, overstrike);
  197.   menu_icheck(menubar, WINSTYLE, sliders);
  198.   menu_icheck(menubar, WINTITLE, titles);
  199. }
  200.  
  201. /* This function writes the config file name in directory path.
  202.  */
  203. write_config (path, name)
  204. char    *path;
  205. char    *name;
  206. {
  207.     char    file[100];
  208.     char    *ptr;
  209.     FILE    *fd;
  210.     int    i;
  211.     char    *rindex();
  212.   
  213.     strcpy(file, path);
  214.     ptr = rindex(file, '\\');
  215.     if (ptr != NULL)
  216.         *(ptr + 1) = '\0';
  217.     strcat(file, name);
  218.   
  219.     fd = fopen(file, "w");
  220.     if (fd == NULL)
  221.     {
  222.         form_alert(1, "[2][Can't open config output file][ok]");
  223.         return;
  224.     }
  225.  
  226.     fprintf(fd, "audibell=%d\n", audibell);
  227.     fprintf(fd, "visibell=%d\n", visibell);
  228.     fprintf(fd, "topbell=%d\n", toponbel);
  229.     fprintf(fd, "%s\n", fast? "fast": "slow");
  230.     fprintf(fd, "%s\n", overstrike? "overstrike": "nooverstrike");
  231.     fprintf(fd, "%s\n", sliders? "sliders": "nosliders");
  232.     fprintf(fd, "%s\n", titles? "titles": "notitles");
  233.     fprintf(fd, "bufsiz=%ld\n", bufsizs[bufsiz]);
  234.     fprintf(fd, "cmdpath=%s\n", cmdpath);
  235.     fprintf(fd, "cmdname=%s\n", cmdname);
  236.     fprintf(fd, "cmdargs=%s\n", cmdargs);
  237.     for (i = 0; i < fontsavail; i++)
  238.         if (curfont == fnttbl[i])
  239.             fprintf(fd, "font=%d\n", i);
  240.  
  241.     for (i = 0; i < NFSTRINGS - 2; ++i)
  242.         fprintf(fd, "f%d=%s\n", i + 1, fstrings[i]);
  243.     fclose(fd);
  244. }
  245.  
  246. setcapture (wp)
  247. WI_STR    *wp;
  248. {
  249.     char    full[80];
  250.     register char    *cp1, *cp2;
  251.  
  252.     if (wp->wi_lfd)
  253.     {
  254.         fclose(wp->wi_lfd);
  255.         wp->wi_lfd = NULL;
  256.     }
  257.     if (*wp->wi_fname)
  258.     {
  259.         cp1 = full;
  260.         for (cp2 = wp->wi_fpath; *cp2; *cp1++ = *cp2++);
  261.         while (--cp1 >= full && *cp1 != '\\');
  262.         strcpy(++cp1, wp->wi_fname);
  263.         if ((wp->wi_lfd = fopen(full, "a")) == NULL)
  264.         {
  265.             form_alert(1, "[2][Unable to open capture file.][ok]");
  266.             *wp->wi_fname = '\0';
  267.         }
  268.     }
  269.     w_rename(wp - w, NULL);
  270. }
  271.  
  272. #define    DEFUCR    0x88        /* Async, 1 stop bit */
  273.  
  274. int    speedobjs[] = {
  275.     BB1920,    BB960,    BB480,    BB360,    BB240,    BB200,    BB180,    BB120,
  276.     BB60,    BB30,    BB20,    BB15,    BB13,    BB11,    BB7,    BB5
  277. };
  278. int    flowobjs[] = {
  279.     FCBNONE,    FCBXON,        FCBRTS
  280. };
  281. int    parobjs[] = {
  282.     PBNONE,        PBODD,        PBEVEN
  283. };
  284. int    stopobjs[] = {
  285.     SBB1,        SBB15,        SBB2
  286. };
  287. int    bcbobjs[] = {
  288.     BCB8,        BCB7,        BCB6,        BCB5
  289. };
  290. int    bufobjs[] = {
  291.     BUFSIZ1,    BUFSIZ2,    BUFSIZ32
  292. };
  293. #ifdef    GETSPEED
  294. /* Baud rates 75 and 50 are not currently decoded. */
  295. int    timevals[] = {
  296.     1,    2,    4,    5,    8,    10,    11,
  297.     16,    32,    64,    96,    128,    143,    175
  298. };
  299. #endif
  300.  
  301. int    speed = -1;        /* Don't know */
  302. int    flow = -1;
  303. int    ucr = -1;
  304. struct iorec old_iorec;
  305.  
  306. /* load rs232 configuration into RSCONF dialog */
  307. getrsconf ()
  308. {
  309.     OBJECT    *obj;
  310.     register int    i;
  311. #ifdef    GETSPEED
  312. #define    MFP    0xFFFFFA01L
  313. #define    TDDR    36
  314. #define    hz_200    ((unsigned long *) 0x4BAL)
  315.     int    tv;            /* Timer value */
  316.     int    maxtv;            /* Maximum timer value */
  317.     unsigned long    endhz;        /* End of polling period */
  318.     long    savessp;        /* Old stack pointer */
  319. #endif
  320. #ifdef    GETFLOW
  321.     struct    rsiorec    {
  322.         char    fill0[32];
  323.         char    rsmode;
  324.         char    fill1[1];
  325.     };
  326. #endif
  327.  
  328. #ifdef    GETSPEED
  329.     savessp = Super(0L);
  330.     maxtv = 0;
  331.     endhz = *hz_200 + 8;
  332.     while (*hz_200 < endhz)
  333.     {
  334.         tv = *((unsigned char *) (0xFFFFFA01L + 36));
  335.         if (tv > maxtv)
  336.             maxtv = tv;
  337.     }
  338.     (void) Super(savessp);
  339.     for (i = sizeof(timevals) / sizeof(timevals[0]);
  340.       --i >= 0 && timevals[i] != maxtv; );
  341.     if (i >= 0)
  342.         speed = i;
  343. #endif
  344. #ifdef    GETFLOW
  345.     flow = ((struct rsiorec *) Iorec(0))->rsmode;
  346.     if (flow == 3)            /* UW doesn't support both */
  347.         flow = 1;
  348. #endif
  349. #ifdef    GETUCR
  350.     /* Dev. Kit code suggests that Rsconf() returns old register vals */
  351.     ucr = (xbios(15, -1, -1, -1, -1, -1, -1) >> 24) & 0xFF;
  352. #endif
  353.     rsrc_gaddr(R_TREE, RSCONF, &obj);
  354.     for (i = sizeof(speedobjs) / sizeof(int); --i >= 0; )
  355.         obj[speedobjs[i]].ob_state = NORMAL;
  356.     for (i = sizeof(flowobjs) / sizeof(int); --i >= 0; )
  357.         obj[flowobjs[i]].ob_state = NORMAL;
  358.     for (i = sizeof(parobjs) / sizeof(int); --i >= 0; )
  359.         obj[parobjs[i]].ob_state = NORMAL;
  360.     for (i = sizeof(stopobjs) / sizeof(int); --i >= 0; )
  361.         obj[stopobjs[i]].ob_state = NORMAL;
  362.     for (i = sizeof(bcbobjs) / sizeof(int); --i >= 0; )
  363.         obj[bcbobjs[i]].ob_state = NORMAL;
  364.     for (i = sizeof(bufobjs) / sizeof(int); --i >= 0; )
  365.         obj[bufobjs[i]].ob_state = NORMAL;
  366.  
  367.     if (speed >= 0)
  368.         obj[speedobjs[speed]].ob_state = SELECTED;
  369.  
  370.     if (flow >= 0)
  371.         obj[flowobjs[flow]].ob_state = SELECTED;
  372.  
  373.     if (ucr >= 0)
  374.     {
  375.         switch (ucr & 0x6)    /* Parity */
  376.         {
  377.         default:
  378.             obj[PBNONE].ob_state = SELECTED;
  379.             break;
  380.         case 0x4:
  381.             obj[PBODD].ob_state = SELECTED;
  382.             break;
  383.         case 0x6:
  384.             obj[PBEVEN].ob_state = SELECTED;
  385.             break;
  386.         }
  387.     
  388.         switch (ucr & 0x18)
  389.         {
  390.         default:
  391.             obj[SBB1].ob_state = SELECTED;
  392.             break;
  393.         case 0x10:
  394.             obj[SBB15].ob_state = SELECTED;
  395.             break;
  396.         case 0x18:
  397.             obj[SBB2].ob_state = SELECTED;
  398.             break;
  399.         }
  400.     
  401.         obj[bcbobjs[(ucr >> 5) & 3]].ob_state = SELECTED;
  402.     }
  403.     obj[bufobjs[bufsiz]].ob_state = SELECTED;
  404. }
  405.     
  406. /* set rs232 configuration from the RSCONF dialog */
  407. setrsconf ()
  408. {
  409.     register int    i;
  410.     OBJECT    *obj;
  411.     short    parity = 0;
  412.     short    stop = 0;
  413.     short    bcb = -1;
  414.  
  415.     rsrc_gaddr(R_TREE, RSCONF, &obj);
  416.  
  417.     for (i = sizeof(speedobjs) / sizeof(int); --i >= 0; )
  418.         if (obj[speedobjs[i]].ob_state & SELECTED)
  419.         {
  420.             speed = i;
  421.             break;
  422.         }
  423.  
  424.     for (i = sizeof(flowobjs) / sizeof(int); --i >= 0; )
  425.         if (obj[flowobjs[i]].ob_state & SELECTED)
  426.         {
  427.             flow = i;
  428.             break;
  429.         }
  430.  
  431.     for (i = sizeof(parobjs) / sizeof(int); --i >= 0; )
  432.         if (obj[parobjs[i]].ob_state & SELECTED)
  433.         {
  434.             parity = i + 1;
  435.             break;
  436.         }
  437.  
  438.     for (i = sizeof(stopobjs) / sizeof(int); --i >= 0; )
  439.         if (obj[stopobjs[i]].ob_state & SELECTED)
  440.         {
  441.             stop = i + 1;
  442.             break;
  443.         }
  444.  
  445.     for (i = sizeof(bcbobjs) / sizeof(int); --i >= 0; )
  446.         if (obj[bcbobjs[i]].ob_state & SELECTED)
  447.         {
  448.             bcb = i;
  449.             break;
  450.         }
  451.  
  452.     for (i = sizeof(bufobjs) / sizeof(int); --i >= 0; )
  453.         if (obj[bufobjs[i]].ob_state & SELECTED)
  454.         {
  455.             bufsiz = i;
  456.             break;
  457.         }
  458.  
  459.     if ((parity || stop || bcb >= 0) && ucr < 0)
  460.         ucr = DEFUCR;
  461.     if (parity)
  462.         ucr = (ucr & ~0x6) | (parity << 1);
  463.     if (stop)
  464.         ucr = (ucr & ~0x18) | (stop << 3);
  465.     if (bcb >= 0)
  466.         ucr = (ucr & ~0x60) | (bcb << 5);
  467.  
  468.     Rsconf(speed, flow, ucr, -1, -1, -1);
  469.     rsbufset(bufsiz);
  470. }
  471.  
  472. void rsbufset(bufsiz)
  473. int bufsiz;
  474. {
  475.     struct iorec *ioptr;
  476.     static char    *memalloced = NULL;
  477.     static int    oldbufsiz = 0;
  478.  
  479.     ioptr = Iorec(0);
  480.     if (old_iorec.io_bufsiz == 0)
  481.         old_iorec = *ioptr;    /* save gem buffer */
  482.     if (oldbufsiz != bufsiz) {
  483.         char *mem;
  484.         oldbufsiz = bufsiz;
  485.         mem = (char *) Malloc(bufsizs[bufsiz]);
  486.         if (mem != NULL) {
  487.             if (memalloced != NULL)
  488.                 Mfree(memalloced);
  489.             memalloced = mem;
  490.             ioptr->io_buff = mem;
  491.             ioptr->io_bufsiz = bufsizs[bufsiz];
  492.             ioptr->io_head = 0;
  493.             ioptr->io_tail = 0;
  494.             ioptr->io_low = bufsizs[bufsiz] / 4;
  495.             ioptr->io_high = ioptr->io_bufsiz - ioptr->io_low;
  496.         }
  497.     }
  498. }
  499.